<?
require_once("../config/dbconn.php");require_once("../config/usercls.php");

$sqlhy="select * from {$db_prefix}users where id='".$_SESSION['sys_userid']."'";
$rshy=$db->get_one($sqlhy);

/*$sqlzm="select * from {$db_prefix}applyzmd1 where name=''";
$rszm=$db->get_one($sqlzm);*/
if ($action=='buy'){
	$msg='';
	//if(trim($type)=='') $msg="请选择订单类型\\n";

	if ($msg!=''){
		echo "<script>alert('{$msg}');history.back();</script>";exit();
	}
	$goodcartstr=$_COOKIE['sys_goodscart'];
	if (!$goodcartstr){
		echo "<script>alert('购物车里没有选购产品。\\n\\n请先选购');history.back();</script>";exit();
	}
	//$allprice=0;$allpv=0;
	$allscprice=0;$allgcprice=0;
	if ($goodcartstr){
		$goodcartary=explode('|',$goodcartstr);
		foreach($goodcartary as $k1=>$v1){
			$goodcartary1=explode(',',$v1);
			//插入到订单里边
			$sqlp="select * from {$db_prefix}products where id='".$goodcartary1[0]."'";
			$rsp=$db->get_one($sqlp);
			//$allprice+=$rsp['price']*intval($goodcartary1[1]);
			$allscprice+=$rsp['scprice']*intval($goodcartary1[1]);
			$allgcprice+=$rsp['gcprice']*intval($goodcartary1[1]);
      $price = $allgcprice/$glo_dpjrate_1/$glo_dpjmax_2;
            $gwprice = $price/(1+$glo_dpjmax_3);
            $jjprice = $price-$gwprice;
			//$allpv+=$rsp['pv']*intval($goodcartary1[1]);
		}

		//插入到orders
		unset($dataArray);
		$dataArray['username']=$_SESSION['sys_username'];
		$dataArray['scprice']=$allscprice;
		$dataArray['price']=$gwprice;   //购物币
		$dataArray['gcprice']=$allgcprice;
		$dataArray['pv']=$jjprice;      //奖金币
		$dataArray['addtime']=$curtime;
		$dataArray['state']=0;
		$dataArray['type']=$type;
		//$dataArray['zhekou']=$zhekou;
		$dataArray['sjname']='';
		$dataArray['sjaddress']='';
		$dataArray['contact']='';
		$dataArray['memo']='';
		//$dataArray['bduser']=$bduser;
		//$dataArray['zmduser']=$zmduser;
		$db->insert("{$db_prefix}orders",$dataArray);
		$orderid=$db->insert_id();
		//插入到orders1
		foreach($goodcartary as $k1=>$v1){
			$goodcartary1=explode(',',$v1);
			//插入到订单里边
			$sqlp="select * from {$db_prefix}products where id='".$goodcartary1[0]."'";
			$rsp=$db->get_one($sqlp);
			unset($dataArray);
			$dataArray['username']=$_SESSION['sys_username'];
			$dataArray['orderid']=$orderid;
			$dataArray['proid']=$rsp['id'];
			$dataArray['productname']=$rsp['productname'];
			$dataArray['scprice']=$rsp['scprice'];
			$dataArray['price']=$gwprice; //  购物币  
			$dataArray['pv']=$jjprice;       //  奖金币
			$dataArray['gcprice']=$rsp['gcprice'];
			$dataArray['num']=$goodcartary1[1];
			$dataArray['addtime']=$curtime;
			//$dataArray['bduser']=$bduser;
			//$dataArray['zmduser']=$zmduser;
			$db->insert("{$db_prefix}orders1",$dataArray);		
			
		}
	}
	//清空购物车
	setcookie('sys_goodscart','');
	
	echo "<script>alert('订单提交成功\\n\\n请尽快支付订单');location.href='order_lst0.php';</script>";exit();
}


if ($action=='clear'){
	setcookie('sys_goodscart','');
	
	echo "<script>location.href='product_cart.php';</script>";exit();
}

if ($action=='del'){
	editgoodscart($pid,0);
	
	echo "<script>location.href='product_cart.php';</script>";exit();
}

if ($action=='update'){
	$goodcartstr=$_COOKIE['sys_goodscart'];
	if ($goodcartstr){
		$goodcartary=explode('|',$goodcartstr);
		foreach($goodcartary as $k1=>$v1){
			$goodcartary1=explode(',',$v1);
			editgoodscart($goodcartary1[0],$_POST["num_{$goodcartary1[0]}"]);
		}
	}
	echo "<script>location.href='product_cart.php';</script>";exit();
}


?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script language="javascript">
function updatecart(){
	var frm=document.form1;
	frm.submit();
}
function buydo(){
	var frm=document.form1;
	frm.action='?action=buy';
	frm.submit();
}
</script>
<link href="images/skin.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="17" height="29" valign="top" background="images/mail_leftbg.gif"><img src="images/left-top-right.gif" width="17" height="29" /></td>
    <td width="935" height="29" valign="top" background="images/content-bg.gif"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
      <tr>
        <td height="31"><div class="titlebt">购物车</div></td>
      </tr>
    </table></td>
    <td width="16" valign="top" background="images/mail_rightbg.gif"><img src="images/nav-right-bg.gif" width="16" height="29" /></td>
  </tr>
  <tr>
    <td height="71" valign="middle" background="images/mail_leftbg.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F7F8F9"><table width="100%" height="138" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="13" valign="top">&nbsp;</td>
      </tr>
      <tr>
        <td valign="top">
		<form name="form1" id="form1" method="post" action="?action=update">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td height="30" colspan="2">
                <table width="100%" height="60" border="0" cellpadding="0" cellspacing="1" bgcolor="silver">
                  <tr>
                    <td align="center" bgcolor="#f2f2f2" class="left_txt">商品名称</td>
                    <td align="center" bgcolor="#f2f2f2" class="left_txt">市场价</td>
                    <!--<td align="center" bgcolor="#f2f2f2" class="left_txt">购物券</td>-->
                    <td align="center" bgcolor="#f2f2f2" class="left_txt">会员价</td>
                    <td align="center" bgcolor="#f2f2f2" class="left_txt">爱华购物币</td>
                    <td align="center" bgcolor="#f2f2f2" class="left_txt">奖金币</td>
                   <!-- <td align="center" bgcolor="#f2f2f2" class="left_txt">赠送积分</td>
                   -->
                    <td align="center" bgcolor="#f2f2f2" class="left_txt">购买数量</td>
                    <td align="center" bgcolor="#f2f2f2" class="left_txt">购物币小计</td>
                    <!--<td align="center" bgcolor="#f2f2f2" class="left_txt">购物券小计</td>
                    -->
                    <td height="30" align="center" bgcolor="#f2f2f2" class="left_txt">操作</td>
                  </tr>
                  <tbody bgcolor="#FFFFFF">
				<?
				$allprice=0;$allscprice=0;
				$goodcartstr=$_COOKIE['sys_goodscart'];
				if ($goodcartstr){
					$goodcartary=explode('|',$goodcartstr);
					foreach($goodcartary as $k1=>$v1){
						$goodcartary1=explode(',',$v1);
						$sqlp="select * from {$db_prefix}products where id='".$goodcartary1[0]."'";
						$rsp=$db->get_one($sqlp);
            $price = $rsp['gcprice']/$glo_dpjrate_1/$glo_dpjmax_2;
            $gwprice = $price/(1+$glo_dpjmax_3);
            $jjprice = $price-$gwprice;
				?>
                    <tr>
                      <td align="center" class="left_txt"><a href="../uploads/productimgs/<?=$rsp['pimg']?>" target="_blank"><img src="../uploads/productimgs/<?=$rsp['pimg']?>" border="0" width="100"></a><br>
                          <a href="product_view.php?id=<?=$rsp['id']?>">
                            <?=$rsp['productname']?>
                          </a></td>
                      <td align="center" class="left_txt"><?=$rsp['scprice']?>
                        人民币</td>
                      <td align="center" class="left_txt"><?=$rsp['gcprice']?>
                        人民币</td>
                      <td align="center" class="left_txt"><?=(int)$gwprice?>YLB</td>
                      <td align="center" class="left_txt"><?=(int)$jjprice?>YLB</td>  
                        
                      <td align="center" class="left_txt"><label>
                        <input name="num_<?=$rsp['id']?>" type="text" id="num_<?=$rsp['id']?>" size="5" value="<?=$goodcartary1[1]?>">
                      </label></td>
                      <td align="center" class="left_txt"><? 
                      /*$allprice+=$rsp['price']*$goodcartary1[1];*/
					  $allscprice+=$rsp['gcprice']*$goodcartary1[1];
					  echo ($rsp['gcprice']*$goodcartary1[1]);
					  ?>
                        人民币</td>

                      <td height="30" align="center" class="left_txt"><A href="?action=del&pid=<?=$rsp['id']?>"><img src="images/pic22.gif" width="17" height="15" border="0">删除</A></td>
                    </tr>
                    <?
					}
				}else{
				?>
                    <tr>
                      <td align="center" class="left_txt">&nbsp;</td>
                      <td align="center" class="left_txt">&nbsp;</td>
                      <td align="center" class="left_txt">&nbsp;</td>
                      <td align="center" class="left_txt">&nbsp;</td>
                      <td align="center" class="left_txt">&nbsp;</td>
                      <td align="center" class="left_txt">&nbsp;</td>
                      <td height="30" align="center" class="left_txt"><A href=""></A></td>
                    </tr>
                    <?
				}
				?>
                  </tbody>
                </table>
				</td>
            </tr>
            <tr style="display:">
              <td height="30" align="right">&nbsp;</td>
              <td height="30" align="left">&nbsp;</td>
            </tr>
            <tr>
              <td height="30" align="left" class="red">购物积分￥<?=$allscprice?>人民币</td>
              <td width="44%" height="30" align="right"><a href="?action=clear"><img src="images/gw.gif" width="16" height="16" border="">清空购物车</a>
                <a href="javascript:updatecart();"><img src="images/gw2.gif" width="16" height="16" border="0">更新购物车</a></td>
            </tr>
          <!--  <tr>
              <td height="30" align="left" >服务中心：<input name="bduser" type="text" id="bduser" value="<?=$rshy['bduser']?>" /></td>  
              <td height="30" align="left" >专卖店：<input name="zmduser" type="text" id="zmduser" value="<?=$rszm['username']?>"/></td>            
            </tr>-->
            
            <tr>
              <td height="30">&nbsp;</td>
              <td height="30" align="right">&nbsp;</td>
            </tr>
            <tr>
              <td height="30"><a href="shopIndex.php" target="_blank"><img src="images/continue.gif" width="125" height="40" border="0"></a></td>
              <td height="30" align="right"><a href="javascript:buydo();"><img src="images/goumai2.gif" border="0" width="120" height="36"></a></td>
            </tr>
          </table>
		 </form>
		  </td>
      </tr>
    </table></td>
    <td background="images/mail_rightbg.gif">&nbsp;</td>
  </tr>
  <tr>
    <td valign="middle" background="images/mail_leftbg.gif"><img src="images/buttom_left2.gif" width="17" height="17" /></td>
      <td height="17" valign="top" background="images/buttom_bgs.gif"><img src="images/buttom_bgs.gif" width="17" height="17" /></td>
    <td background="images/mail_rightbg.gif"><img src="images/buttom_right2.gif" width="16" height="17" /></td>
  </tr>
</table>

</body>
</html>